package com.zbaohuang.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.zbaohuang.model.VisitorRecordModel;
import com.zbaohuang.view.CircleImageView;
import com.zbaohuang.xyh.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zbaohuang on 16/3/26.
 */
public class VisitorRecordAdapter extends UltimateViewAdapter<RecyclerView.ViewHolder> {

    private List<VisitorRecordModel> recordModels;

    public VisitorRecordAdapter(List<VisitorRecordModel> recordModels) {
        this.recordModels = recordModels;
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View view) {
        return new UltimateRecyclerviewViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_visitor_record, viewGroup, false);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public int getAdapterItemCount() {
        return recordModels.size();
    }

    @Override
    public long generateHeaderId(int position) {

        if (hasHeaderView()) position--;

        Log.e("generateHeaderId", "position ==" + position + " " + recordModels.get(position).getIntDateTime() + "");
        return recordModels.get(position).getIntDateTime();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        if (holder instanceof  ViewHolder){
            ViewHolder viewHolder = (ViewHolder)holder;
            if (position == 0) {

                viewHolder.moshengren.setVisibility(View.GONE);
                viewHolder.moshengren2.setVisibility(View.VISIBLE);
                viewHolder.moshengren3.setVisibility(View.VISIBLE);
                viewHolder.unreadTextView.setText("2");
                viewHolder.unreadTextView2.setText("1");
                viewHolder.unreadTextView3.setText("1");
                viewHolder.timeTextView.setText("10:30");
                viewHolder.timeTextView2.setText("11:30");
                viewHolder.timeTextView3.setText("17:30");
                viewHolder.visitorImageView1.setImageResource(R.mipmap.yangming);
                viewHolder.visitorImageView2.setImageResource(R.mipmap.avatar2);
                viewHolder.visitorImageView3.setImageResource(R.mipmap.avatar1);

            } else if (position == 1) {

                viewHolder.moshengren.setVisibility(View.GONE);
                viewHolder.moshengren2.setVisibility(View.GONE);
                viewHolder.moshengren3.setVisibility(View.GONE);
                viewHolder.unreadTextView.setText("2");
                viewHolder.unreadTextView2.setText("6");
                viewHolder.unreadTextView3.setText("3");
                viewHolder.timeTextView.setText("10:30");
                viewHolder.timeTextView2.setText("11:30");
                viewHolder.timeTextView3.setText("17:30");
                viewHolder.visitorImageView1.setImageResource(R.mipmap.man1);
                viewHolder.visitorImageView2.setImageResource(R.mipmap.avatar3);
                viewHolder.visitorImageView3.setImageResource(R.mipmap.lilianjie);

            } else if (position == 2) {


                viewHolder.moshengren.setVisibility(View.GONE);
                viewHolder.moshengren2.setVisibility(View.GONE);
                viewHolder.moshengren3.setVisibility(View.GONE);
                viewHolder.unreadTextView.setText("5");
                viewHolder.unreadTextView2.setText("1");
                viewHolder.unreadTextView3.setText("8");
                viewHolder.timeTextView.setText("10:30");
                viewHolder.timeTextView2.setText("11:30");
                viewHolder.timeTextView3.setText("17:30");
                viewHolder.visitorImageView1.setImageResource(R.mipmap.pingming1);
                viewHolder.visitorImageView2.setImageResource(R.mipmap.woman);
                viewHolder.visitorImageView3.setImageResource(R.mipmap.pingming2);

            }
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_visitor_header, viewGroup, false);

        HeaderViewHolder viewHolder = new HeaderViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        String date = "02日\r\n04月";

        if (position == 0 ) {
            date = "05日\r\n04月";

            String visitorCount = "访客数:3人";
            String moshengCount = "陌生访客:2人";
            SpannableString s1 = new SpannableString(visitorCount);
            s1.setSpan(new AbsoluteSizeSpan(40), 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            //将这个Span应用于指定范围的字体
            s1.setSpan(span, 4, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ((HeaderViewHolder) viewHolder).countTextView.setText(s1);

            SpannableString s2 = new SpannableString(moshengCount);
            s2.setSpan(new AbsoluteSizeSpan(40), 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //将这个Span应用于指定范围的字体
            s2.setSpan(span, 5, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ((HeaderViewHolder) viewHolder).moshengTextView.setText(s2);

        } else if (position == 1) {
            date = "04日\r\n04月";

            String visitorCount = "访客数:3人";
            String moshengCount = "陌生访客:0人";
            SpannableString s1 = new SpannableString(visitorCount);
            s1.setSpan(new AbsoluteSizeSpan(40), 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            //将这个Span应用于指定范围的字体
            s1.setSpan(span, 4, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ((HeaderViewHolder) viewHolder).countTextView.setText(s1);

            SpannableString s2 = new SpannableString(moshengCount);
            s2.setSpan(new AbsoluteSizeSpan(40), 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //将这个Span应用于指定范围的字体
            s2.setSpan(span, 5, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ((HeaderViewHolder) viewHolder).moshengTextView.setText(s2);

        } else {
            String visitorCount = "访客数:3人";
            String moshengCount = "陌生访客:0人";
            SpannableString s1 = new SpannableString(visitorCount);
            s1.setSpan(new AbsoluteSizeSpan(40), 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            //将这个Span应用于指定范围的字体
            s1.setSpan(span, 4, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ((HeaderViewHolder) viewHolder).countTextView.setText(s1);

            SpannableString s2 = new SpannableString(moshengCount);
            s2.setSpan(new AbsoluteSizeSpan(40), 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //将这个Span应用于指定范围的字体
            s2.setSpan(span, 5, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            ((HeaderViewHolder) viewHolder).moshengTextView.setText(s2);
        }
        SpannableString spannableString = new SpannableString(date);
        spannableString.setSpan(new AbsoluteSizeSpan(30), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((HeaderViewHolder) viewHolder).dateTextView.setText(spannableString);
    }


    class HeaderViewHolder extends UltimateRecyclerviewViewHolder {

        TextView dateTextView;
        TextView countTextView;
        TextView moshengTextView;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            dateTextView = (TextView) itemView.findViewById(R.id.headerDatetimeTextView);
            countTextView = (TextView) itemView.findViewById(R.id.visitorCountTextView);
            moshengTextView = (TextView) itemView.findViewById(R.id.mosCountTextView);
        }
    }


    static class ViewHolder extends UltimateRecyclerviewViewHolder {
        @Bind(R.id.visitorImageView1)
        CircleImageView visitorImageView1;
        @Bind(R.id.unreadTextView)
        TextView unreadTextView;
        @Bind(R.id.timeTextView)
        TextView timeTextView;
        @Bind(R.id.moshengren)
        ImageView moshengren;
        @Bind(R.id.visitorImageView2)
        CircleImageView visitorImageView2;
        @Bind(R.id.unreadTextView2)
        TextView unreadTextView2;
        @Bind(R.id.timeTextView2)
        TextView timeTextView2;
        @Bind(R.id.moshengren2)
        ImageView moshengren2;
        @Bind(R.id.visitorImageView3)
        CircleImageView visitorImageView3;
        @Bind(R.id.unreadTextView3)
        TextView unreadTextView3;
        @Bind(R.id.timeTextView3)
        TextView timeTextView3;
        @Bind(R.id.moshengren3)
        ImageView moshengren3;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
